package com.kzd.gtboon.jetpack.vm

import android.app.Application
import androidx.lifecycle.MutableLiveData
import com.kzd.gtboon.base.BaseViewModel
import com.kzd.gtboon.entity.*
import com.kzd.gtboon.jetpack.api.*
import com.module.lemlin.http.*
import com.module.lemlin.owner.setRxLifeScope

class MineViewModel(application: Application) : BaseViewModel(application) {

    private var page: Int = 1
    private var limit: Int = 10

    val couponPriceLiveData = MutableLiveData<HttpResponseBody<HttpResponseDataE<WelfareCoupon>>>()
    fun mineCouponPrice(type: Int, isLoadMore: Boolean = false) {
        setRxLifeScope(object : SubscribeViewModel<HttpResponseDataE<WelfareCoupon>>(couponPriceLiveData) {
            override fun onSuccess(resp: HttpResponseDataE<WelfareCoupon>) {
                super.onSuccess(resp.apply {
                    this.isLoadMore = isLoadMore
                })
                page += 1
            }
        }) {
            if (!isLoadMore) {
                page = 1
            }
            userMyCoupon(
                type.toString(), page.toString(), limit.toString()
            )
        }
    }

    val giftListMyLiveData = MutableLiveData<HttpResponseBody<HttpResponseDataE<GameGift>>>()
    fun mineGiftList(keyword: String? = null, isLoadMore: Boolean = false) {
        setRxLifeScope(object : SubscribeViewModel<HttpResponseDataE<GameGift>>(giftListMyLiveData) {
            override fun onSuccess(resp: HttpResponseDataE<GameGift>) {
                super.onSuccess(resp.apply {
                    this.isLoadMore = isLoadMore
                })
                page += 1
            }
        }) {
            if (!isLoadMore) {
                page = 1
            }
            personalMyGift(
                keyword, page.toString(), limit.toString()
            )
        }
    }
}